package hikari.persistence.propertieseditor.editors;

import com.gooddies.texteditors.validation.AbstractValidator;
import hikari.persistence.file.FileSection;
import java.io.IOException;
import javax.swing.JCheckBox;
import javax.swing.JComponent;

/**
 * @author sad
 */
public class BooleanPropertyEditor extends JCheckBox implements IPropertyEditor {

    @Override
    public Object getEditorValue() {
        return isSelected();
    }

    @Override
    public void setEditorValue(Object value) {
        setSelected((Boolean) value);
    }

    @Override
    public void setMax(Object max) {
        throw new UnsupportedOperationException("Boolean property editor does not support max property");
    }

    @Override
    public void setMin(Object min) {
        throw new UnsupportedOperationException("Boolean property editor does not support min property");
    }

    @Override
    public void setValidator(AbstractValidator validator) {
        throw new UnsupportedOperationException("Boolean property editor does not support validator property");
    }

    @Override
    public void setMaxLength(String length) {
        throw new UnsupportedOperationException("Boolean property editor does not support length property");
    }

    @Override
    public JComponent getJComponent() {
        return this;
    }

    @Override
    public void save(String fieldName, FileSection section) throws IOException {
        section.writeBoolean(fieldName, (Boolean) getEditorValue());
    }

    @Override
    public void read(String fieldName, FileSection section) throws IOException {
        Boolean value=section.readBoolean(section.getChunk(fieldName));
        setEditorValue(value);
    }
}
